/**
 * 
 */
package ptvv.automatons;

import ptvv.system.DefPortAutomaton;
import ptvv.system.InternalStates;
import ptvv.system.ThreadAndClass;

/**
 * @author dkz
 * 
 */
public class Delay extends DefPortAutomaton {
	// Private Variables

	public Delay(ThreadAndClass dest) {
		this.setName("NoNameDelay");
		this.setOutputPort(dest, 0, 0);

	}

	public Delay(String name) {
		this.setName(name);

	}

	public void run() {
		while (internalState != InternalStates.ReallyDead)
			runStates();

		// logger.log("SHUTDOWN");
	}

	public void starting() throws InterruptedException {
		// System.out.println("waiting");
		String item = eventQueue.take();
		// System.out.println(item);

	}

	public void ready() throws InterruptedException {
		String temp = inputPorts.get(0).queue.poll();
		if (temp != null)
			message = temp;
		else
			message = "1"; // default delay
	}

	public void running() throws InterruptedException {
		// Thread.sleep(Long.parseLong(message));
		System.out.println("sleeping..");

		Thread.sleep(Long.parseLong(message) * 1000);

		System.out.println("awake");
	}

	public void suspended() throws InterruptedException {

	}

	public void end() {
		System.out.println("end");
		logger.log("" + this.getName() + " : " + message);
		outputPorts.get(0).interruptThread();
		//outputPorts.get(0).destThread.interrupt();
		// repeatBool = true;index
	}

	public void dead() {
	

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

}
